package leetcode;

public class LeetCode999 {
    public int numRookCaptures1(char[][] board) {
        int x = 0, y = 0, result = 0, tmp = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 'R') {
                    x = i;
                    y = j;
                }
            }
        }
        tmp = y;
        while (tmp > 0) {
            if (board[x][tmp] == 'B')
                break;
            if (board[x][tmp] == 'p') {
                result++;
                break;
            }
            tmp--;
        }
        tmp = y;
        while (tmp < board.length) {
            if (board[x][tmp] == 'B')
                break;
            if (board[x][tmp] == 'p') {
                result++;
                break;
            }
            tmp++;
        }
        tmp = x;
        while (tmp > 0) {
            if (board[tmp][y] == 'B')
                break;
            if (board[tmp][y] == 'p') {
                result++;
                break;
            }
            tmp--;
        }
        tmp = x;
        while (tmp < board.length) {
            if (board[tmp][y] == 'B')
                break;
            if (board[tmp][y] == 'p') {
                result++;
                break;
            }
            tmp++;
        }
        return result;
    }

    //------------------------------------------------------------------
    public int numRookCaptures(char[][] board) {
        int x = 0, y = 0, result = 0, tmp = 0;
        //可以使用数组方向 上 右 下 左
        int[] dx = new int[]{0, 1, 0, -1};
        int[] dy = new int[]{1, 0, -1, 0};
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 'R') {
                    x = i;
                    y = j;
                }
            }
        }
        for (int i = 0; i < 4; ++i) {
            for (int step = 0; ; ++step) {
                int tx = x + step * dx[i];
                int ty = y + step * dy[i];
                if (tx < 0 || tx >= 8 || ty < 0 || ty >= 8 || board[tx][ty] == 'B') break;
                if (board[tx][ty] == 'p') {
                    result++;
                    break;
                }
            }
        }
        return result;
    }
}
